package zhou.cutfruit.thread;

import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;
import zhou.cutfruit.view.GameView;
/**
 * 不断刷新界面的线程
 * @author Administrator
 *
 */
public class DrawThread extends Thread {
	GameView gameView;
	SurfaceHolder surfaceHolder;
	int sleepSpan = 10;
	long start = System.nanoTime();
	int count = 0;
	public boolean flag;

	static final int MAX_COUNT = 3000;

	Object threadWaitObject;

	public DrawThread(GameView gameView, SurfaceHolder holder) {
		this.gameView = gameView;
		this.surfaceHolder = holder;
		this.flag = true;
		threadWaitObject = new Object();
	}

	@Override
	public void run() {
		Canvas canvas = null;
		while (flag) {

			// 最后替换掉判断游戏结束的条件，替换掉
			count++;
			if (count == MAX_COUNT) {
				gameView.setGameOver(true);
			}

			try {
				//canvas = surfaceHolder.lockCanvas(null);
				synchronized (surfaceHolder) {
					//gameView.doSendEvent(gameView.getMotionEvent());
					canvas = surfaceHolder.lockCanvas();
					gameView.doDraw(canvas);
					gameView.drawKnifePath(canvas);
					if (count == 200) {
						// gameView.doDrawJam(canvas);
						Log.i("zhou", "绘制果酱");
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (canvas != null) {
					surfaceHolder.unlockCanvasAndPost(canvas);
				}
			}

			try {
				Thread.sleep(sleepSpan);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// 若isGameOver为true ， 则循环结束
			if (gameView.isGameOver()) {
				gameView.startSelectionView();
			}

		}
	}

}
